//
//  DHProjectCell.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/5/5.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHProjectCell.h"
#import "DHButtonCell.h"
#import "DHButton.h"
NSString *buttonCellID = @"buttonCellID";
@interface DHProjectCell()<UICollectionViewDataSource,UICollectionViewDelegate, DHButtonCellDelegate>
@property(nonatomic ,strong)UICollectionView *projectView;
@property(nonatomic ,strong)UICollectionViewFlowLayout *layout;
@property(nonatomic ,strong)UIButton *currentBtn;
//@property(nonatomic ,strong)UIButton *currentBtn2;
//@property(nonatomic ,strong)DHButtonCell *currentCell1;
@property(nonatomic ,strong)DHButtonCell *currentCell;

@end

@implementation DHProjectCell

#pragma mark: - 懒加载 layout
- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _layout;
}

- (void)setPlanList:(NSArray *)planList{
    _planList = planList;
    
    if (!planList){return;}
    [self setupUIWithArray:planList];
    
}

- (void)setPlanInfo:(DHPlanInfo *)planInfo{
    _planInfo = planInfo;
    
    if (!planInfo){return;}
    [self setupUIWithArray:planInfo.class_list];}

- (void)setupUIWithArray:(NSArray *)array{
    NSInteger numOfRow = (array.count % 3 == 0) ? array.count / 3 : array.count / 3 + 1;
    //设置projectView属性
    self.projectView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.projectView.scrollEnabled = NO;
    self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    self.layout.sectionInset = UIEdgeInsetsMake(10, 5,5, 10);
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.itemSize = CGSizeMake((self.bounds.size.width) / 3, (self.bounds.size.height) / numOfRow);
    [self.contentView addSubview:self.projectView];
    self.projectView.backgroundColor = UIColor.whiteColor;
    [self.projectView reloadData];

}

#pragma mark: - 懒加载 projectView
- (UICollectionView *)projectView{
    if (!_projectView) {
        _projectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _projectView.dataSource = self;
        _projectView.delegate = self;
    }
    return _projectView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    [self.projectView registerNib:[UINib nibWithNibName:@"DHButtonCell" bundle:nil] forCellWithReuseIdentifier:buttonCellID];
    return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
}

#pragma mark: - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger itemsCount = self.planList ? self.planList.count : self.planInfo.class_list.count;
    return itemsCount;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DHButtonCell *cellbtn = [collectionView dequeueReusableCellWithReuseIdentifier:buttonCellID forIndexPath:indexPath];

    cellbtn.planList = self.planList ? self.planList[indexPath.row] : nil;
    cellbtn.classModel = self.planInfo ? self.planInfo.class_list[indexPath.row] : nil;
    cellbtn.delegate = self;
    return cellbtn;
}
- (void)buttonCellClick:(DHButtonCell *)cell sender:(DHButton *)sender{

    sender.selected = YES;
    self.currentBtn.selected = (self.currentCell != cell) ? NO : YES;
    self.currentBtn = sender;
    self.currentCell = cell;
    
 
    if (sender.planList) {
        [NSNotificationCenter.defaultCenter postNotificationName:@"kChangeProjectNotifcationName"
                                                                            object:nil
                                                                            userInfo:@{@"planList":sender.planList}];
        
    }else{
        [NSNotificationCenter.defaultCenter postNotificationName:@"kChangeProjectNotifcationName"
                                                          object:nil
                                                        userInfo:@{@"classifyModel":sender.classModel}];
    }
//
//    switch (self.tag) {
//        case 1:
//            sender.selected = YES;
//            self.currentBtn1.selected = (self.currentCell1 != cell) ? NO : YES;
//            self.currentBtn1 = sender;
//            self.currentCell1 = cell;
//            break;
//         case 2:
//            sender.selected = YES;
//            self.currentBtn2.selected = (self.currentCell2 != cell) ? NO : YES;
//            self.currentBtn2 = sender;
//            self.currentCell2 = cell;
//            break;
//        default:
//            break;
//    }
    
  //  self.currentCell.classifyButton =
    
    
}

#pragma mark: - UICollectionViewDelegate
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"点到了");
//}


@end
